﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TableModule
{
    public static class TableManager
    {
        static public TableSystemSetting tableSystemSetting;
        static public TableData tableData;
        static public TableData globaltableData;
        static public TableActioner tableActioner;
        static public bool bThreadStart = false;
        static public bool bOpenedSettingForm;
        static public FormTableSetting settingForm;
        static public TableRunner tableRunner;
        //new and Module Load will Call This
        static public void Init()
        {
            tableSystemSetting = TableSystemSetting.Load();
            tableData = TableData.Load();
            globaltableData = TableData.LoadGlabal();
            InitTableActions();
            if (bThreadStart == false)
            {
                System.Threading.Thread threadScan = new System.Threading.Thread(TableScanThread);
                threadScan.Name = "TableScanThread";
                bThreadStart = true;
                threadScan.IsBackground = true;
                threadScan.Start();

            }
            FrameManager.FrameManager.saveProject += FrameManager_saveProject;
            FrameManager.FrameManager.saveAsProject += FrameManager_saveAsProject;
            FrameManager.FrameManager.openProject += FrameManager_openProject;
        }

        private static bool FrameManager_openProject()
        {
            ReOpen();
            return true;
        }

        private static bool FrameManager_saveAsProject()
        {
            Save();
            return true;
        }

        private static bool FrameManager_saveProject()
        {
            Save();
            return true;
        }

        //Opne will Call this
        static public void ReOpen()
        {
            tableData = TableData.Load();
            InitTableActions();
        }
        static public void InitTableActions()
        {
            
            tableActioner = new TableActioner(tableSystemSetting.tableSetting, tableData.TableSingleData, globaltableData.TableSingleData);
            tableRunner = new TableRunner(tableActioner);


        }
        static public void Save()
        {
            tableSystemSetting.Save();
            tableData.Save();
            globaltableData.SaveGlabal();
        }
       
        static public TableActioner Table(string strName)
        {
            return tableActioner;
        }
        static private void TableScanThread()
        {
            TableActioner actioner = TableManager.tableActioner;
            while (true)
            {
                try
                {
                   
                        actioner.GetCCWL(AxisName.X);
                        actioner.GetCCWL(AxisName.Y);
                        actioner.GetCCWL(AxisName.Z);
                        actioner.GetCCWL(AxisName.U);

                        actioner.GetCWL(AxisName.X);
                        actioner.GetCWL(AxisName.Y);
                        actioner.GetCWL(AxisName.Z);
                        actioner.GetCWL(AxisName.U);

                        actioner.GetAlarm(AxisName.X);
                        actioner.GetAlarm(AxisName.Y);
                        actioner.GetAlarm(AxisName.Z);
                        actioner.GetAlarm(AxisName.U);
                    
                    //MoveDoneManager.MoveDoneManager.AxisMoveDoneScanFunction();
                    //MoveDoneManager.MoveDoneManager.AxisMoveDoneScanFunction();
                }
                catch
                {

                }
                System.Threading.Thread.Sleep(50);
            }

            //return ;
        }
        public static Form OpenSettingForm()
        {
            FormTableSetting frmSetting = new FormTableSetting();
            frmSetting.TopLevel = false;
            return frmSetting;
        }

        public static Form OpenDedugForm()
        {
            FormDebug frmSetting = new FormDebug();
            frmSetting.TopLevel = false;
            return frmSetting;
        }
    }
}
